package com.igeek.utils;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * @author jerry
 */
public class SendSMS {
//    @Value("${signName}")
//    public static String signName;
//    @Value("${templateCode}")
//    public static String templateCode;
    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    public static String send(String mobile) throws Exception {
        //产生4位随机数
        String code = "";
        Random random = new Random();
        for(int i=0;i<6;i++){
            code+=random.nextInt(10);
        }
        com.aliyun.dysmsapi20170525.Client client = SendSMS.createClient("LTAI4FwN5xPZSbnwdsyyCxZK", "ZcVUNtLjzK1AhtwIlPhoBadWoyS6rX");
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(mobile)
                .setSignName("极客营")
                .setTemplateCode("SMS_173766180")
                .setTemplateParam("{\"code\":"+code+"}");
        // 复制代码运行请自行打印 API 的返回值
        client.sendSms(sendSmsRequest);
        return  code;
    }
}
